<template>
  <div>
    <div class="login-page">
      <p>用户登录</p>
      <form>
        <input
          type="text"
          v-model="phone"
          placeholder="请输入手机号码"
          ref="phone"
        />
        <br />
        <input type="password" v-model="password" placeholder="请输入密码" />
        <br />
        <p @click="userLogin" ref="login" class="login">登录</p>
      </form>
      <p ref="res1" class="res1">手机号码不合法</p>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      phone: "17612045791",
      password: "",
      newUser:[]
    };
  },
  methods: {
    userLogin() {
      if (!this.checkPhone()) return;
      console.log(this.phone, this.password);
      this.$http
        .post(
          "/login/cellphone?phone=" + this.phone + "&password=" + this.password
        )
        .then((data) => {
          this.newUser = data.data.profile
          console.log(this.newUser);
          window.localStorage.setItem('user',JSON.stringify(this.newUser))
          window.localStorage.setItem('isLogin',true)
          this.$router.push('/my')
        })
        .catch((err) => {
          console.log("err:", err);
        });
    },
    checkPhone() {
      const regPhone = /^[1][3,4,5,7,8][0-9]{9}$/; //电话号码正则
      if (!regPhone.test(this.phone)) {
        setTimeout(() => {
          this.$refs.res1.style.opacity = "0";
        }, 2000);
        this.$refs.res1.style.opacity = "1";
        return false;
      }
      return true;
    },
  },
};
</script>

<style lang="less" scoped>
.login-page {
  height: calc(100vh - 10rem);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  p {
    color: #8a898e;
    font-size: 30px;
    text-align: center;
  }
  .login {
    position: relative;
    top: 10vh;
    height: 3rem;
    width: 18rem;
    margin: 0.5rem 0;
  }
  input {
    border: solid 2px #8a898e;
    border-radius: 5px;
    height: 3rem;
    width: 18rem;
    margin: 1rem 0;
    text-align: center;
    font-size: 1rem;
  }
  .res,
  .res1 {
    width: 100%;
    top: 62%;
    font-size: 1.5rem !important;
    position: absolute;
    opacity: 0;
    text-align: center;
    transition: 0.5s;
  }
}
</style>